﻿using Painter.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Painter.Properties;

namespace Painter.Common
{
    public class ReadInput
    {
        /// <summary>
        /// 文件路径
        /// </summary>
        private static string _filePath;

        /// <summary>
        /// 打开文件选择框设置文件路径返回选择结果
        /// </summary>
        public static bool OpenFileDialog()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "输入文本文件（*.txt）|*.txt";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                _filePath = ofd.FileName;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 根据文件路径打开文本返回文本内容
        /// </summary>
        /// <returns></returns>
        public static string GetStr()
        {
            string str = null;
            if (_filePath != null)
            {
                StreamReader sr = new StreamReader(_filePath, Encoding.UTF8);
                str = sr.ReadToEnd();
                ReadInput.ReadDraw(str);
                sr.Close();
            }
            _filePath = null;
            return str;
        }

        /// <summary>
        /// 读取文本中的画图数据
        /// </summary>
        /// <param name="str">文本字符流</param>
        public static void ReadDraw(string str)
        {
            //换行符用斜杠代替,方便读取
            str = str.Replace("\r\n", "/");
            //以#分出每组排样结果
            string[] datas = str.Split('#');

            //循环读取每组排样结果
            for (int i = 1; i < datas.Length; i++)
            {
                //输出模型初始化
                var output = new Output();
                //以!分出一组排样结果的每种排样方式
                string[] data = datas[i].Split('!');
                var dataNum = data.Length;
                //初步判断文本格式是否正确
                if (dataNum < 3)
                {
                    MessageBox.Show(Resources.ReadInput_Read_导入的文本数据格式错误);
                    return;
                }
                //排样的计算数据
                output.Data = data[0];
                //排样的计算结果
                output.Result = data[dataNum - 1];
                output.Plates = new List<List<List<int[]>>>();

                #region!标志分隔的每种排样方式

                for (int j = 1; j < dataNum - 1; j++)
                {
                    //一种排样方式
                    output.Plate = new List<List<int[]>>();
                    //一个排样方式的所有数据
                    string[] plate = data[j].Split('/');
                    //所有椭圆的数据
                    output.Ellipses = new List<int[]>();
                    //所有直线的数据
                    output.Lines = new List<int[]>();
                    //所有矩形的数据
                    output.Rectangles = new List<int[]>();
                    //一个方式中的一条排
                    for (int k = 1; k < plate.Length - 1; k++)
                    {
                        string[] shape = plate[k].Split(' ');
                        //将字符串数组转成整数数组
                        var res = Array.ConvertAll(shape, o => (int)double.Parse(o));
                        switch (shape[0])
                        {
                            case "1":
                                output.Rectangles.Add(res);
                                break;
                            case "2":
                                output.Ellipses.Add(res);
                                break;
                            case "3":
                                output.Lines.Add(res);
                                break;
                        }
                    }
                    output.Plate.Add(output.Ellipses);
                    output.Plate.Add(output.Lines);
                    output.Plate.Add(output.Rectangles);
                    output.Plates.Add(output.Plate);

                }

                #endregion

                //将每种排样结果添加到所有画图数据的全局静态变量中
                Form1.Outputs.Add(output);
            }
        }

        /// <summary>
        /// 读取文本中的计算数据
        /// </summary>
        /// <param name="str"></param>
        public static void ReadCalc(string str)
        {
            str = str.Trim('\r', '\n', '#');
            var inputStrs = str.Split('#');
            foreach (var item in inputStrs)
            {
                if (item != null)
                {
                    ReadData(item);
                }
            }
        }

        /// <summary>
        /// 读取计算所需的本文数据
        /// </summary>
        public static void ReadData(string dataString)
        {
            try
            {
                Models.Input input = new Models.Input();
                dataString = dataString.Trim('\r', '\n');
                var a = dataString.Trim('[', ']');
                var b = a.Replace("]\r\n[", "/");
                var c = b.Split('/');
                int i = 0;
                //板材长宽及毛胚种类数
                var d = c[i++].Split(',');
                if (d.Length != 3)
                {
                    MessageBox.Show("数据格式错误！");
                    return;
                }
                input.Length = double.Parse(d[0]);
                input.Width = double.Parse(d[1]);
                input.Num = int.Parse(d[2]);
                input.Diameter = new double[input.Num];
                input.Demand = new int[input.Num];
                int num = int.Parse(d[2]);
                for (int j = 0; j < num; j++)
                {
                    //毛胚规格及数量
                    var e = c[i++].Split(',');
                    input.Diameter[j] = double.Parse(e[0]);
                    input.Demand[j] = int.Parse(e[1]);
                }
                Form1.Inputs.Add(input);
            }
            catch (Exception)
            {
                MessageBox.Show("数据格式错误！");
            }

        }
    }
}
